package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.BeanConv;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author ：yuanjian
 * @date ：Created in 2020/11/14 19:15
 * @description：
 */
@Service(timeout = 60000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
//    @Transactional
    public void batchOrderSetting(List<String[]> list) {
        List<OrderSetting> orderSettingList = new ArrayList<>();
        for (String[] row : list) {
            String orderDate = row[0];
            Integer number = Integer.parseInt(row[1]);
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_date", orderDate);
            OrderSetting orderSetting = this.getOne(queryWrapper);
            if(orderSetting==null){
                orderSetting = new OrderSetting();
//                orderSetting.setOrderDate(DateUtils.parseString2Date(orderDate));
                orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
            }
            orderSetting.setNumber(number);
            orderSettingList.add(orderSetting);
        }
        this.saveOrUpdateBatch(orderSettingList);
    }

    @Override
    public void updateOrderSettingData(String orderDate, Integer num) {
        baseMapper.updateOrderSetting(num,orderDate);
    }

    @Override
    public Integer isOrderOkByOrderDate(String orderDate) {
        return baseMapper.isOrderOkByOrderDate(orderDate);
    }

    @Override
    public void updateReservationsByOrderDate(String orderDate) {
        baseMapper.updateReservationsByOrderDate(orderDate);
    }

    @Override
    public void CleanOrderSetting() {
        baseMapper.deleteOrderSetting();
    }

    @Override
    public Map findSettingData(String year, String month) {
        //   "2020-09-28":{number:100,reservations:90}
        String  beginDate  = year+"-"+month+"-1";
        String  endDate  =  year+"-"+month+"-31";
        List<OrderSetting> orderSettingList =  baseMapper.findOrderSettingsData(beginDate,endDate);
        Map<String,OrderSetting> map = new HashMap<String,OrderSetting>();
        for(OrderSetting orderSetting : orderSettingList){
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());
            map.put(orderDate,orderSetting);
        }
        return map;
    }
}
